﻿Imports System
Imports Microsoft.VisualBasic
Imports System.Linq
Imports System.Windows.Forms
Imports System.IO
Imports Publics


Public Class FrmDangNhap
    ''' <summary>
    '''  
    ''' </summary>
    ''' <remarks></remarks>
    Private _IsThoat As Boolean = False
    Public Property IsThoat() As Boolean
        Get
            Return _IsThoat
        End Get
        Set(ByVal value As Boolean)
            _IsThoat = value
        End Set
    End Property
    Private intCount As Integer = 0

    Private Sub llblquenmatkhau_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs)
        'If Trim(Me.txttendangnhap.Text) = "" Then
        '    vMsg.Show("Chưa nhập tên đăng nhập", "Thong bao", vMsg.vButtons.vOK, vMsg.vIcon.vInformation)
        '    txttendangnhap.Focus()
        '    Exit Sub
        'End If
        'Dim usernamme As String = (From ds In dbTV.DM_NhanViens _
        '                                 Where ds.UserName = Trim(txttendangnhap.Text) _
        '                                 Select ds.UserName).FirstOrDefault
        'If usernamme <> "" Then
        '    vFrmGuiPass = New FrmGuiPass
        '    vFrmGuiPass.tendangnhap = Trim(txttendangnhap.Text)
        '    vFrmGuiPass.ShowDialog()

        'ElseIf txttendangnhap.Text = "" Then
        '    vMsg.Show(GetTextFromResx("MsgUsernameIsNotInput"), GetTextFromResx("ApplicationName"), vMsg.vButtons.vOK, vMsg.vIcon.vInformation)
        'Else
        '    vMsg.Show(GetTextFromResx("MsgUsernameIsInvalid"), GetTextFromResx("ApplicationName"), vMsg.vButtons.vOK, vMsg.vIcon.vInformation)
        '    txttendangnhap.Focus()
        '    txttendangnhap.SelectAll()
        'End If
    End Sub


    Private Sub Btndangnhap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btndangnhap.Click
        Try
            Dim sThongBao As String = CheckSave()
            If sThongBao <> "" Then
                MsgBox(sThongBao)
                Exit Sub
            End If
            Dim tendangnhap As String = Trim(txttendangnhap.Text)
            Dim matkhau As String = Trim(txtmatkhau.Text)
            Dim usernamme As String = (From ds In dbTV.DM_NhanViens Where ds.UserName = tendangnhap Select ds.UserName).FirstOrDefault
            ' Kiểm tra tên đăng nhập
            If usernamme Is Nothing Then
                txttendangnhap.Focus()
                txttendangnhap.SelectAll()
                MsgInfo("Bạn nhập sai tên đăng nhập")
                intCount += 1
                If intCount = 3 Then
                    MsgInfo("Bạn nhập sai mật khẩu quá 3 lần, chương trình sẽ thoát")
                    vUser = Nothing
                    Me.Close()
                    Me.IsThoat = True
                Else
                    Exit Sub
                End If
            Else ' Kiểm tra mật khẩu truy cập
                Dim objNSD As DM_NhanVien = (From ds In dbTV.DM_NhanViens _
                                             Where ds.UserName = tendangnhap And ds.PassWord = Vla.Vms.ClsUtil.MD5Encrypt(matkhau) _
                                             And ds.InUsed _
                                             Select ds).FirstOrDefault
                If objNSD IsNot Nothing AndAlso Not objNSD.KichHoat Then
                    intCount += 1
                    If intCount = 3 Then
                        MsgInfo("Bạn nhập sai mật khẩu quá 3 lần, chương trình sẽ thoát")
                        vUser = Nothing
                        Me.Close()
                        Me.IsThoat = True
                    Else
                        MsgInfo("Tài khoản chưa được kích hoạt")
                        Exit Sub
                    End If
                ElseIf objNSD Is Nothing Then
                    txtmatkhau.Focus()
                    txtmatkhau.SelectAll()
                    MsgInfo("Sai mật khẩu")
                    intCount += 1
                    If intCount = 3 Then
                        MsgInfo("Bạn nhập sai mật khẩu quá 3 lần, chương trình sẽ thoát")
                        vUser = Nothing
                        Me.Close()
                        Me.IsThoat = True
                    End If
                ElseIf objNSD IsNot Nothing And objNSD.KichHoat Then
                    vUser = New clsNguoiDung
                    vUser.CanBo = objNSD
                    vUser.DonVi = (From ds In dbTV.DM_DonVis Where ds.InUsed AndAlso ds.ID = objNSD.ID_DonVi Select ds).FirstOrDefault
                    vUser.Quyen = GetPermissions(vUser.CanBo.ID)
                    SaveLog("Đăng nhập", "Đăng nhập", vUser.CanBo.UserName, vUser.CanBo.ID)
                    Me.Close()
                End If
            End If
        Catch ex As Exception
            MsgErr(ex.Message)
        End Try
    End Sub

    Private Function CheckSave() As String
        Dim ErrorStr As String = ""
        If Trim(Me.txttendangnhap.Text) = "" Then
            ErrorStr = "Chưa nhập tên đăng nhập"
            txttendangnhap.Focus()
        ElseIf Trim(Me.txtmatkhau.Text) = "" Then
            ErrorStr = "Chưa nhập mật khẩu"
            txtmatkhau.Focus()
        End If
        CheckSave = ErrorStr
    End Function

    Private Sub FrmDangNhap_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Me.Text = "Đăng nhập"
        intCount = 0

        Dim strMAC As String = Vla.Vms.ClsUtil.MD5Encrypt(GetMACAddress())

        
        If strMAC = "145f5bb90bdd06be756609e85e9c9e41" Then
            Dim strTenFile As String = Application.StartupPath & "\login.txt"
            If File.Exists(strTenFile) Then
                Dim F As New StreamReader(strTenFile)
                Dim MaHoa As New clsEncrypt
                txttendangnhap.Text = MaHoa.DeCrypt(F.ReadLine)
                txtmatkhau.Text = MaHoa.DeCrypt(F.ReadLine)
            End If
        End If

    End Sub

    Private Sub Btndangnhap_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Btndangnhap.KeyDown
        If e.KeyCode = Keys.Enter Then
            Btndangnhap_Click(Nothing, Nothing)
        End If
    End Sub

    Private Sub BtnHuyBo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnHuyBo.Click
        _IsThoat = True
        Me.Close()
    End Sub

    Private Sub txttendangnhap_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txttendangnhap.KeyDown
        If e.KeyCode = Keys.Enter Then
            txtmatkhau.Focus()
        End If
    End Sub

    Private Sub txtmatkhau_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtmatkhau.KeyDown
        If e.KeyCode = Keys.Enter Then
            Btndangnhap_Click(Nothing, Nothing)
        End If
    End Sub

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub
     
    Private Sub lblMayChu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblMayChu.Click
        'Dim frm As New FrmFirstConfig
        'frm.ShowDialog()
        'ConnectToDB()
    End Sub

    Private Sub lblMayChu_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles lblMayChu.MouseLeave
        'DevExpress.XtraPrinting.Native.CursorStorage.SetCursor(Cursors.No)
    End Sub

    Private Sub lblMayChu_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lblMayChu.MouseMove
        ' DevExpress.XtraPrinting.Native.CursorStorage.SetCursor(Cursors.Hand)
    End Sub
     
End Class